package com.kkb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-09-20 9:02
 * @Modified By:
 */
@Controller
@RequestMapping("/file")
public class FileController {
    @RequestMapping("/upload")
    public String upload(@RequestParam("file1") MultipartFile multipartFile, HttpServletRequest req) throws IOException {
        System.out.println("进来了");
        //1.获取原来的文件名称
        String on= multipartFile.getOriginalFilename();
        //2.重新命名
        String filename = UUID.randomUUID().toString().replace("-","")+on.substring(on.lastIndexOf("."));
        //3.添加到服务器端的路径
        String path = req.getServletContext().getRealPath("/uploadFile")+"/";
        //上传
        multipartFile.transferTo(new File(path+filename));
        System.out.println(path+filename);
        return "ok";
    }
}
